﻿using System;
using System.Web;

namespace msBsc.Cls
{
    //using DotNet.Common.Model;
    //using DotNet.Common.Util;

    public class BPageHt : BPage
    {

        public override void Dispose()
        {
            base.Dispose();
            //htBtTipMsg.Dispose();
        }

        //public msBsc.AppLogic.Cfg.UserInfo oUserInfo = new msBsc.AppLogic.Cfg.UserInfo();

        public BPageHt()
        {
        }


        public void SetLogin(HttpContext opage, bool blRedirect)
        {
            if (opage == null)
            {
                opage = HttpContext.Current;
            }
            //string strPage = opage.Request.Path.ToUpper().Substring(opage.Request.Path.LastIndexOf("/") + 1);

            //if (Request.Path.ToUpper().IndexOf("WFLOGIN.ASPX") != -1
            //    || Request.Path.ToUpper().IndexOf("WFREGISTER.ASPX") != -1)
            //{
            //    return;
            //}

            //0后台用户，1客户，2地接客
            if (msBsc.SvRef.UserInfo.OperType != "0")
            {
                msBsc.SvRef.UserInfo.ExitSys();
                //throw new Exception("你没有权限访问！");
            }
            if (!msBsc.SvRef.UserInfo.Online)
            {
                if (!string.IsNullOrEmpty(opage.Request.Params["SETLOGIN"]))
                {
                    string strSETLOGIN = opage.Request.Url.ToString();
                    strSETLOGIN = strSETLOGIN.Substring(strSETLOGIN.IndexOf("SETLOGIN=") + 9, strSETLOGIN.Length - strSETLOGIN.IndexOf("SETLOGIN=") - 9);
                    string strLogin = msBsc.AppLogic.Cfg.DesCry.encMe(strSETLOGIN, "zbhscsys");

                    string strActDt = this.GetParamValue(strLogin, "ACTDT");
                    if (strActDt != string.Empty && DateTime.Parse(strActDt).CompareTo(DateTime.Now.AddMinutes(15)) > 0)
                    {
                        string strUser = this.GetParamValue(strLogin, "USER");
                        string strPass = this.GetParamValue(strLogin, "PASS");
                        msBsc.SvRef.UserInfo.Login(strUser, strPass);
                    }
                }
                else //if(!IsPostBack && !IsCallback)
                {
                    if (IsPostBack || isAjaxLoad)
                    {
                        throw new Exception("会话或服务器缓存已过期，请重新登录！");
                    }
                    if (blRedirect)
                    {
                        //ToPageRegister("wfLogin.aspx");
                        string strRetUrl = opage.Request.RawUrl;
                        int itm1 = strRetUrl.IndexOf("?");
                        if (itm1 > 0)
                        {
                            strRetUrl = strRetUrl.Substring(0, itm1) + "?" + opage.Request.QueryString.ToString();
                        }
                        opage.Response.Redirect(msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase + "s_Login.htm?ReturnUrl=" + strRetUrl);
                    }
                }
            }
        }
        //public string ValitateSeaSystem(msBsc.SvRef.IcssService.ShareServiceClient objClient)
        //{
        //    string strUser = msBsc.AppLogic.Cfg.ApplicationConst.GetAppSettings("SeaSystemUserNme");
        //    string strPass = msBsc.AppLogic.Cfg.ApplicationConst.GetAppSettings("SeaSystemUserPass");
        //    string strMsg= objClient.LoginWCF(strUser, strPass
        //        , Epm.AppLogic.Framework.ApplicationInfo.operRank
        //        , Epm.AppLogic.Framework.ApplicationInfo.Operator
        //        , Epm.AppLogic.Framework.ApplicationInfo.OperatorName
        //        , Epm.AppLogic.Framework.ApplicationInfo.OperatorOffice
        //        , Epm.AppLogic.Framework.ApplicationInfo.OperatorOfficeNme);
        //    if (strMsg != "OK")
        //    {
        //        throw new Exception(strMsg);
        //    }
        //    return strMsg;
        //}
        //public string GetCurrUserPass()
        //{
        //    if (HttpContext.Current.User.Identity.IsAuthenticated)
        //    {
        //        string userName = HttpContext.Current.User.Identity.Name;
        //        userName = Epm.AppLogic.Framework.CUtil.DecryptionStringdata(userName, Epm.AppLogic.Framework.CUtil.EncryptionType.RiJ);
        //        if (HttpContext.Current.User.Identity.IsAuthenticated)
        //        {
        //            string strUser = this.GetParamValue(userName, "USER");
        //            string strPass = this.GetParamValue(userName, "PASS");
        //            return strPass;
        //        }
        //    }
        //    return "";
        //}

        public string GetCmpLoginString(bool blCurrLogin)
        {
            HttpServerUtility Server = HttpContext.Current.Server;
            string strUser = msBsc.SvRef.UserInfo.Operator;
            string strPass = "";// this.OperatorPass;
            if (!blCurrLogin)
            {
                strUser = msBsc.SvRef.ApplicationConst.Const.GetAppSettings("CmpSystemUserNme");
                strPass = msBsc.SvRef.ApplicationConst.Const.GetAppSettings("CmpSystemUserPass");
            }
            string strRet = "SETLOGIN=" + msBsc.AppLogic.Cfg.DesCry.uncMe("USER|" + strUser + ";PASS|" + strPass + ";ACTDT|" + DateTime.Now.ToString(), "zbhscsys");//("USER|" + strUser + ";PASS|" + strPass + ";ACTDT|" + DateTime.Now.ToString(), CUtil.EncryptionType.RiJ);
            return strRet;
        }

        //public string GetSeaLoginString()
        //{
        //    HttpServerUtility Server = HttpContext.Current.Server;
        //    string strRet = "SETLOGIN=" + Epm.AppLogic.Framework.CUtil.EncryptionStringData("USER|" + msBsc.AppLogic.Cfg.ApplicationConst.GetAppSettings("SeaSystemUserNme") + ";PASS|" + msBsc.AppLogic.Cfg.ApplicationConst.GetAppSettings("SeaSystemUserPass"), CUtil.EncryptionType.RiJ);
        //    return strRet;
        //}

        //public string GetCmpSystemPage(string strvPagePath)
        //{
        //    HttpServerUtility Server = HttpContext.Current.Server;
        //    if (strvPagePath.StartsWith("/"))
        //        strvPagePath = strvPagePath.Substring(1, strvPagePath.Length - 1);
        //    string strSeaHttp = msBsc.AppLogic.Cfg.ApplicationConst.GetAppSettings("CmpSystemHttpPath");
        //    if (!strSeaHttp.EndsWith("/"))
        //        strSeaHttp = strSeaHttp + "/";
        //    string strRet = strSeaHttp + strvPagePath;
        //    strRet = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase + "UserControl/GetHtml.aspx?TAMP=" + msBsc.SvRef.ApplicationConst.Const.GenerateStrId() + "&P_URL=" + this.Server.UrlEncode(strRet);
        //    return strRet;
        //}


        public void AllowExecAction(string strAction)
        {
            AllowExecAction(strAction, true);
        }

        public bool GetAllowExecAction(string strAction)
        {
            return AllowExecAction(strAction, false) == "1";
        }

        public string AllowExecAction(string strAction, bool blErr)
        {
            ////string strPUrl = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;
            ////if (strPUrl.StartsWith("~/"))
            ////{
            ////    strPUrl = strPUrl.Substring(2, strPUrl.Length - 2);
            ////}
            //////string strPage = Request.Path.Substring(Request.Path.LastIndexOf("/") + 1);
            ////strPUrl = this.GetType().Name;
            //msBsc.AppLogic.Bse.CBizSysUser objSysUser = new msBsc.AppLogic.Bse.CBizSysUser();
            //if (!objSysUser.AllowAccessRight(this.Operator, strPageUi, strAction))
            //{
            //    if (blErr)
            //        throw new Exception("NO GRANT，对不起，你没有操作的权限！");
            //    else
            //        return "";
            //}
            return "1";
        }
        //private msBsc.AppLogic.Cfg.SynDictionary htBtTipMsg = new msBsc.AppLogic.Cfg.SynDictionary(false);
        //private msBsc.AppLogic.Cfg.SynDictionary htBtTipMsg
        //{
        //    get
        //    {
        //        msBsc.AppLogic.Cfg.SynDictionary _httem1;
        //        if (!this.SessionIsExists(sCurrPageRadom + "_MSG1", "3"))//Session[sCurrPageRadom + "_MSG1"] == null
        //        {
        //            _httem1 = new msBsc.AppLogic.Cfg.SynDictionary(false);
        //            SetSession(sCurrPageRadom + "_MSG1",_httem1,"3");
        //        }
        //        else
        //        {
        //            _httem1 = (msBsc.AppLogic.Cfg.SynDictionary)GetSession(sCurrPageRadom + "_MSG1", "3");
        //        }
        //        return _httem1;
        //    }
        //}

        public override string GetExecJScript()
        {
            //ShowAllbtTip(true);
            return base.GetExecJScript();
        }

        public override void PreDoAction() //函数名改为 DoAction 界面层传至中间层的所有的操作都由此函数来进行后台处理
        {
            //try
            //{

                SetLogin(null, false);


                if (strAction == "doShowUserJobList")
                {
                    //ShowFwsfUserJobList(GetParamHtValue("ID"));
                }
                //else if (strAction == "doShowTipBytile")
                //{
                //    doShowTipBytile(GetParamHtValue("TITLE_TXT"));
                //}
                //else if (strAction == "doShowPageJobList")
                //{
                //    ShowPageJobList(GetParamHtValue("SHOW_PAGE_JOB_L_ID"));
                //}
                //else if (strAction == "doChangeJobpstautss")
                //{
                //    ChangeJobpstautss(GetParamHtValue("SHOW_PAGE_JOB_L_ID"), GetParamHtValue("SHOW_PAGE_JOB_L_OP"), GetParamHtValue("SHOW_PAGE_JOB_L_NS"));
                //}
                //else if (strAction == "doPageJobBeClick")
                //{
                //    doPageJobBeClick(GetParamHtValue("SHOW_PAGE_JOB_L_ID"), GetParamHtValue("SHOW_PAGE_JOB_L_WID"));
                //} 
            //}
            //catch (Exception err)
            //{
            //    this.Alert("服务器端错误：" + err.Message);
            //}
            base.PreDoAction();
        }

        protected override void OnInit(EventArgs e)
        {
            if (!this.isAjaxLoad)
            {
                SetLogin(null, true);//登录验证
            }
            blAddHtJs = true;
            base.OnInit(e);


            //if (this.strPageUi != "wfHome" && this.strPageUi != "MenuTree")
            //{
            //    UserInfo oUserInfo = new UserInfo();
            //    if (msBsc.SvRef.UserInfo.Online)
            //        msBsc.SvRef.UserInfo.LogUserOperate("访问页面", "访问页面：" + HttpContext.Current.Request.Url.ToString()
            //        , ""
            //        , ""
            //        , HttpContext.Current.Request.UserHostAddress
            //        , HttpContext.Current.Request.UserHostName
            //        , HttpContext.Current.Request.UserAgent
            //        , sCurrPageRadom);
            //}

        }

        protected override void OnUnload(EventArgs e)
        {
            //Control oControlt = this.Page.FindControl("sRadom");
            //if (oControlt != null && oControlt.GetType() == typeof(System.Web.UI.WebControls.HiddenField))
            //{
            //    System.Web.UI.WebControls.HiddenField sRadom = (System.Web.UI.WebControls.HiddenField)oControlt;
            //    sCurrPageRadom = sRadom.Value;
            //}
            //if(this.Controls.f
            base.OnUnload(e);
        }
    }
}